import os
from app import create_app, db
from app.models.department import Department
from app.models.file import File

app = create_app()

# 初始化数据库
# 使用标志确保只初始化一次
initialized = False

@app.before_request
def create_tables():
    global initialized
    if not initialized:
        with app.app_context():
            db.create_all()
            # 添加初始部门数据
            if not Department.query.first():
                departments = [
                    Department(name='大气办'),
                    Department(name='规划科'),
                    Department(name='协调科'),
                    Department(name='办公室'),
                    Department(name='建设科'),
                    Department(name='财务科')
                ]
                db.session.add_all(departments)
                db.session.commit()
        initialized = True

if __name__ == '__main__':
    app.run(debug=True)